home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / arcdif.c next >
Encoding:
C/C++ Source or Header  |  1988-10-15  |  8.3 KB  |  314 lines

  1. void arcdif(buf)
  2. char buf[280];
  3. {
  4. #include "nbstime.h"
  5. #include <stdio.h>
  6. #ifdef IBMPC
  7. #include <dos.h>
  8. #if defined(MSC)
  9. struct dosdate_t date;
  10. struct dostime_t time;
  11. #endif
  12. #endif
  13. #ifdef SUN
  14. #include <sys/time.h>
  15. #include <math.h>
  16. #endif
  17. char c;
  18. int j,yr,mo,day,hr,min,sec,dst;   /* holds parsed NBS time */
  19. #ifdef IBMPC
  20. int yribm,moibm,dayibm,hribm,minibm,secibm,hunibm; /* holds computer time*/
  21. int yrat,moat,dayat,hrat,minat,secat;/* holds CMOS time for AT*/
  22. #endif
  23. #ifdef SUN
  24. long int mjd;
  25. long int mjd0 = 40587;     /* mjd of 1/1/70 */
  26. struct timeval tvv,*tp;
  27. #endif
  28. double diff;
  29. #ifdef IBMPC
  30. extern int utcdif;           /* local time - utc in hours */
  31. extern int dsflag;           /* daylight saving time? 1=yes, 0=no */
  32. extern int atflag;           /* AT-type machine? 1=yes, 0=no */
  33. #endif
  34. extern FILE *jop;            /* file handle for writing difference*/
  35. #ifdef IBMPC
  36. static int lday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /*last day of month*/
  37. static int tday[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
  38. int unpbcd();
  39. int dsdone = 0;              /* flag to show day. sav. time corr. done */
  40. #endif
  41. int done   = 0;              /* flag to show when comparison is done */
  42. /*
  43.         this subroutine receives a time string in character
  44.         array buf.  it is parsed and compared with the system
  45.         clock.  the origin of the parser is the - character between
  46.         the year and the month so that leading stuff (possible a lf)
  47.     will simply be ignored.
  48.  
  49.     if IBMPC is true, then the comparison is made using local
  50.     time as defined by global variables dsflag to check for
  51.         daylight savings time and utcdif to convert from UTC to local
  52.         time. if SUN is true then the conversion is done using UTC
  53.     directly.
  54.  
  55.         begin by getting computer time now.
  56. */
  57. #ifdef IBMPC
  58. #if defined(MSC)
  59.     _dos_gettime(&time);
  60.     hribm=time.hour;
  61.     minibm=time.minute;
  62.     secibm=time.second;
  63.     hunibm=time.hsecond;
  64.     _dos_getdate(&date);
  65.     yribm=date.year;
  66.     moibm=date.month;
  67.     dayibm=date.day;
  68.     yribm -= 1900;
  69. #else
  70.         _AH=0x2c;
  71.         geninterrupt(0x21);
  72.         hribm=_CH;
  73.         minibm=_CL;
  74.         secibm=_DH;
  75.         hunibm=_DL;
  76.         _AH=0x2a;
  77.         geninterrupt(0x21);
  78.         yribm=_CX;
  79.         moibm=_DH;
  80.         dayibm=_DL;
  81.         yribm -= 1900;
  82. /*
  83.         if this is an at-type machine, read CMOS clock too
  84. */
  85.         if(atflag != 0)
  86.         {
  87.         _AH=2;
  88.         geninterrupt(0x1a);
  89.         hrat=_CH;
  90.         minat=_CL;
  91.         secat=_DH;
  92.         _AH=4;
  93.         geninterrupt(0x1a);
  94.         yrat=_CL;
  95.         moat=_DH;
  96.         dayat=_DL;
  97.         }
  98. #endif
  99. #endif
  100. #ifdef SUN
  101.     tp= &tvv;
  102.     gettimeofday(tp,0);
  103. #endif
  104. /*
  105.         now parse line from NBS transmission
  106.  
  107.     first find - between year and month
  108. */
  109.         for(j=0; (buf[j] != 0) && (buf[j] != '-') ; j++) ;
  110.         sscanf(&buf[j-2],"%2d-%2d-%2d %2d:%2d:%2d %d",&yr,&mo,&day,
  111.         &hr,&min,&sec,&dst);
  112. #ifdef SUN
  113.     sscanf(&buf[j-8],"%5ld",&mjd);   /* also get mjd for SUN */
  114. /*
  115.     convert nbs time to number of seconds since 1/1/70
  116. */
  117.     mjd= 86400*(mjd - mjd0);
  118.     mjd= mjd + 3600*hr + 60*min + sec;
  119. #endif
  120. #ifdef IBMPC
  121. /*
  122.     for the IBMPC version, the comparison is made against
  123.     local time -- thus nbs time must be converted to local
  124.     time using utcdif and dst flags.
  125. */
  126. /*
  127.         make standard-time portion of dst flag contiguous
  128. */
  129.         if(dst == 0) dst = 100;
  130. /*
  131.         convert from utc to local time. note that minute and second
  132.         are already correct.
  133.         daylight savings time flag must also be updated
  134.         if conversion to local time changes the day
  135.  
  136.         **
  137.         ** version of 12 may -- compiled and tested 31 may
  138.         daylight savings time correction must be done in two
  139.         parts since adding hour may cause hour/day to overflow.
  140.         therefore deal with part of it now -- if day changes then
  141.         correction flag dst will change and therefore correction
  142.         for transition days must be done after final day is known.
  143.         also see subroutine parset where same problem appears.
  144. */
  145.         if( (yr & 3) == 0 ) lday[2]=29;  /* 29 days for Feb in leap year
  146. */
  147.         hr += utcdif;                    /* convert hour to local time */
  148.         if( (dsflag != 0) && (dst <= 50) && (dst > 1) )
  149.         {
  150.         hr++;
  151.         dsdone=1;
  152.         }              /* do first part of daylight savings time */
  153.         if(hr < 0)
  154.         {
  155.         hr += 24;
  156.         day--;
  157.         dst++;         /* update daylight savings flag for change of day*/
  158.         if(day < 1)
  159.            {
  160.            mo--;
  161.            if(mo < 1)
  162.               {
  163.               mo=12;
  164.               yr--;
  165.               }
  166.            day=lday[mo];
  167.            }
  168.         }
  169.         if(hr > 23)
  170.         {
  171.         hr -= 24;
  172.         day++;
  173.         dst--;        /* update daylight saving flag for change of day */
  174.         if(day > lday[mo])
  175.            {
  176.            day=1;
  177.            mo++;
  178.            if(mo > 12)
  179.               {
  180.               mo=1;
  181.               yr++;
  182.               }
  183.            }
  184.         }
  185. /*
  186.         now finish up daylight savings time if enabled
  187. */
  188.         if ( (dsflag != 0) && (dsdone == 0) )
  189.         {
  190.            if( (dst == 51) && (hr >= 2) ) hr++;
  191.            if( (dst ==  1) && (hr <  2) ) hr++;
  192.         }
  193. #endif
  194. /*
  195.         ready to begin comparing the clocks
  196. */
  197.         fprintf(jop,"%2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
  198.               yr,mo,day,hr,min,sec);
  199. #ifdef SUN
  200. /*
  201.     sun comparison is done directly as difference
  202.     of two longs giving number of seconds since 1/1/70
  203. */
  204.     diff= tp->tv_sec - mjd;
  205.     done=0;
  206.     if(fabs(diff) > 100000.)
  207.        {
  208.        fprintf(jop," %8.2fd\n",diff/86400.);
  209.        done=1;
  210.        }
  211.     if( (done == 0) && (fabs(diff) > 10000.) )
  212.        {
  213.        fprintf(jop," %8.2fh\n",diff/3600.);
  214.        done=1;
  215.        }
  216.     if( (done == 0) && (fabs(diff) > 1000.) )
  217.        {
  218.        fprintf(jop," %8.2fm\n",diff/60.);
  219.        done=1;
  220.        }
  221.     if( (done == 0) && (fabs(diff) <= 1000.) )
  222.        {
  223.        diff += (float) tp->tv_usec/1000000.;
  224.        fprintf(jop," %8.2fs\n",diff);
  225.        }
  226. #endif
  227. #ifdef IBMPC
  228. /* 
  229.     ibm comparison is done bit by bit to preserve
  230.     resolution
  231. */
  232.         diff=365*(yribm - yr) + tday[moibm] - tday[mo] + dayibm - day;
  233.         if( ( (yribm & 3) == 0)  && (moibm > 2) ) diff++;
  234.         if( ( (yr    & 3) == 0)  && (mo    > 2) ) diff--;
  235.         if( (diff > 1) || (diff < -1) )
  236.         {
  237.         fprintf(jop," %8.2fd",diff);
  238.         done=1;
  239.         }
  240.         if(done == 0)
  241.         {
  242.            diff= 24*diff + hribm - hr;
  243.            if( (diff > 2) || (diff < -2) )
  244.            {
  245.               fprintf(jop," %8.2fh",diff);
  246.               done=1;
  247.            }
  248.         }
  249.         if(done == 0)
  250.         {
  251.            diff=60*diff + minibm - min;
  252.            if( (diff > 10) || (diff < -10) )
  253.            {
  254.            fprintf(jop," %8.2fm",diff);
  255.            done=1;
  256.            }
  257.         }
  258.         if(done == 0)
  259.         {
  260.            diff=60*diff + secibm - sec +  0.01*(float)hunibm;
  261.            fprintf(jop," %8.2fs",diff);
  262.         }
  263.         if(atflag == 0)
  264.         {
  265.         fprintf(jop,"     0.00\n");
  266.     fclose(jop);
  267.         return;
  268.         }
  269. /*
  270.         begin comparison of CMOS time -- first convert from packed
  271.         BCD to normal binary, then compare as above.
  272. */
  273.         yrat=unpbcd(yrat);
  274.         moat=unpbcd(moat);
  275.         dayat=unpbcd(dayat);
  276.         hrat=unpbcd(hrat);
  277.         minat=unpbcd(minat);
  278.         secat=unpbcd(secat);
  279.         diff=365*(yrat - yr) + tday[moat] - tday[mo] + dayat - day;
  280.         if( ( (yrat & 3) == 0) && (moat > 2) )  diff++;
  281.         if( ( (yr   & 3) == 0) && (mo   > 2) )  diff--;
  282.         done=0;
  283.         if( (diff > 1) || (diff < -1) )
  284.         {
  285.            fprintf(jop," %8.2fd\n",diff);
  286.            done=1;
  287.         }
  288.         if(done == 0)
  289.         {
  290.         diff=24*diff + hrat - hr;
  291.         if( (diff > 2) || (diff < -2) )
  292.            {
  293.            fprintf(jop," %8.2fh\n",diff);
  294.            done=1;
  295.            }
  296.         }
  297.         if(done == 0)
  298.         {
  299.         diff=60*diff + minat - min;
  300.         if( (diff > 10) || (diff < -10) )
  301.            {
  302.            fprintf(jop," %8.2fm\n",diff);
  303.            done=1;
  304.            }
  305.         }
  306.         if(done == 0)
  307.         {
  308.         diff=60*diff +secat - sec;
  309.         fprintf(jop," %8.2fs\n",diff);
  310.         }
  311. #endif
  312.     fclose(jop);
  313. }
  314.